package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    //批量上传
    @Override
    public void add(List<OrderSetting> list) {
        if (list!=null && list.size()>0){
            for (OrderSetting orderSetting : list) {
                //检查日期是否存在
                long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count>0){
                    //如果存在则进行更新操作
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else {
                    //如何过不存在则执行新增操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    //根据日期修改可预约人数
    @Override
    public void editNumberByOrderDate(OrderSetting orderSetting) {
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count>0){
            //当前日期已经进行了预约设置，此时需要修改操作
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else {
            //当前日期还未进行设置，此时需要添加预约人数
            orderSettingDao.add(orderSetting);
        }
    }

    //获取预约设置数据
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        //分别获取当前月份的第一天和最后一天
        String dateBegin = date+"-1";
        String dateEnd = date+"-31";

        Map map = new HashMap();
        map.put("dateBegin",dateBegin);
        map.put("dateEnd",dateEnd);

       List<OrderSetting> list =  orderSettingDao.getOrderSettingByMonth(map);
       List<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map orderSettingMap = new HashMap();
            //获取日期（日）
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());
            //获取可预约人数
            orderSettingMap.put("number",orderSetting.getNumber());
            //获取已预约人数
            orderSettingMap.put("reservations",orderSetting.getReservations());
            data.add(orderSettingMap);
        }
        return data;
    }
}
